//
//  UpdateHtml.m
//  HealthCA_H5
//
//  Created by 曾华桥 on 17/10/17.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "UpdateHtml.h"
#import "ZipArchive.h"
#import "AFURLSessionManager.h"

@implementation UpdateHtml



//下载zip包
- (void)downFileFromServer:(NSString *)url pro:(void (^)(double index))progress success:(void (^)(NSString *fliePath)) suc filed:(void (^)(NSString *str)) msg{
    
    // 1、 设置请求
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    // 2、初始化
    NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    //设置请求超时为10秒钟
    configuration.timeoutIntervalForRequest = 10;
    AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    // 3、开始下载
    NSURLSessionDownloadTask * task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
        double Progress = downloadProgress.completedUnitCount / (double)downloadProgress.totalUnitCount;
        progress(Progress);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        //这里要返回一个NSURL，其实就是文件的位置路径
        
        NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
//        NSString * path = NSTemporaryDirectory();
//        //使用建议的路径
//        path = [path stringByAppendingPathComponent:@"www"];
        path = [path stringByAppendingPathComponent:response.suggestedFilename];
//        NSLog(@"%@",path);
//        if(![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@www/",NSTemporaryDirectory()]]){
//            // Create "/temp/www" directory
//            NSError *error;
//            NSFileManager *fileManager= [NSFileManager defaultManager];
//            NSURL *temDirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"www"];
//            [fileManager createDirectoryAtURL:temDirURL withIntermediateDirectories:YES attributes:nil error:&error];
//        }
        return [NSURL fileURLWithPath:path];//转化为文件路径
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        //如果下载的是压缩包的话，可以在这里进行解压
        NSLog(@"----%@---%ld---%@",error.domain,error.code,error);
        //下载成功
        if (error == nil) {
            suc([filePath path]);
        }else{//下载失败的时候，只列举判断了两种错误状态码
            NSString * message = nil;
            if (error.code == - 1005) {
                message = @"网络异常";
            }else if (error.code == -1001){
                message = @"请求超时";
            }else{
                message = @"未知错误";
            }
            msg(message);
        }
    }];
    [task resume];
}

- (void)OpenZip:(NSString*)zipPath  unzipto:(NSString*)_unzipto
{
    ZipArchive* zip = [[ZipArchive alloc] init];
    if( [zip UnzipOpenFile:zipPath] )
    {
        BOOL ret = [zip UnzipFileTo:_unzipto overWrite:YES];
        if( NO==ret )
        {
            NSLog(@"error");
        }
        [zip UnzipCloseFile];
    }
}
@end
